//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.jeecg.modules.online.cgreport.c;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.pagination.DialectFactory;
import com.baomidou.mybatisplus.extension.plugins.pagination.DialectModel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.dialects.IDialect;
import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.vo.DynamicDataSourceModel;
import org.jeecg.common.util.ReflectHelper;
import org.jeecg.common.util.dynamic.db.DataSourceCachePool;
import org.jeecg.common.util.dynamic.db.DynamicDBUtil;
import org.jeecg.modules.online.a.a;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Slf4j
public class b {
    private static final Logger d = LoggerFactory.getLogger(b.class);
    public static final String a = " where ";
    public static final String b = " and ";
    public static final String c = " or ";

    public b() {
    }

    public static String a(String var0) {
        var0 = var0.replaceAll("(?i) where ", " where ");
        var0 = var0.replaceAll("(?i) and ", " and ");
        var0 = var0.replaceAll("(?i) or ", " or ");
        String var1 = "(,\\s*|\\s*(\\w|\\.)+\\s*[^, ]+ *\\S*)\\$\\{\\w+\\}\\S*";
        Pattern var2 = Pattern.compile(var1);
        Matcher var3 = var2.matcher(var0);

        while(var3.find()) {
            String var4 = var3.group();
            if (var4.indexOf(" where ") != -1) {
                String var5 = var4.substring(0, var4.indexOf(" where "));
                var0 = var0.replace(var4, var5 + " where 1=1");
            } else if (var4.indexOf(" and ") != -1) {
                var4 = var4.substring(var4.indexOf("and"));
                if (var4.indexOf("(") > 0) {
                    var4 = var4.substring(var4.indexOf("(") + 1);
                    var0 = var0.replace(var4, " 1=1 ");
                } else {
                    var0 = var0.replace(var4, "and 1=1");
                }
            } else if (var4.indexOf(" or ") != -1) {
                var4 = var4.substring(var4.indexOf("or"));
                if (var4.indexOf("(") > 0) {
                    var4 = var4.substring(var4.indexOf("(") + 1);
                    var0 = var0.replace(var4, " 1=1 ");
                } else {
                    var0 = var0.replace(var4, "or 1=1");
                }
            } else if (var4.startsWith(",")) {
                var0 = var0.replace(var4, " ,1 ");
            } else {
                var0 = var0.replace(var4, " 1=1 ");
            }
        }

        var0 = var0.replaceAll("(?i)\\(\\s*1=1\\s*(AND|OR)", "(");
        var0 = var0.replaceAll("(?i)(AND|OR)\\s*1=1", "");
        return var0;
    }

    public static Map<String, Object> a(HttpServletRequest var0) {
        Map var1 = var0.getParameterMap();
        HashMap var2 = new HashMap(5);
        Iterator var3 = var1.entrySet().iterator();
        String var5 = "";
        String var6 = "";

        for(Object var7 = null; var3.hasNext(); var2.put(var5, var6)) {
            Entry var4 = (Entry)var3.next();
            var5 = (String)var4.getKey();
            var7 = var4.getValue();
            if (!"_t".equals(var5) && null != var7) {
                if (!(var7 instanceof String[])) {
                    var6 = var7.toString();
                } else {
                    String[] var8 = (String[])((String[])var7);

                    for(int var9 = 0; var9 < var8.length; ++var9) {
                        var6 = var8[var9] + ",";
                    }

                    var6 = var6.substring(0, var6.length() - 1);
                }
            } else {
                var6 = "";
            }
        }

        return var2;
    }

    public static boolean b(String var0) {
        String var1 = var0.toLowerCase();
        return var1.indexOf("select") == 0;
    }

    public static String c(String var0) {
        final String sql = "select COUNT(1) \"TOTAL\"" + var0.substring(8);
        log.info("COUNT_SQL====>" + sql);
        return sql;
    }

    public static Map<String, Object> a(String var0, String var1) {
        Map var2 = null;
        DynamicDataSourceModel var3 = DataSourceCachePool.getCacheDynamicDataSourceModel(var0);
        DbType var4 = JdbcUtils.getDbType(var3.getDbUrl());
        IDialect var5 = DialectFactory.getDialect(var4);
        if (!var1.toUpperCase().contains("LIMIT") && !var1.toUpperCase().contains("OFFSET")) {
            Page var14 = new Page(1L, 1L);
            DialectModel var7 = var5.buildPaginationSql(var1, var14.offset(), var14.getSize());
            String var8 = var7.getDialectSql();
            if (var8.contains("?")) {
                long var9 = (Long)ReflectHelper.getFieldVal("firstParam", var7);
                long var11 = (Long)ReflectHelper.getFieldVal("secondParam", var7);
                int var13 = var8.length() - var8.replaceAll("\\?", "").length();
                if (var13 == 1) {
                    var2 = (Map)DynamicDBUtil.findOne(var0, var8, new Object[]{var9});
                } else {
                    var2 = (Map)DynamicDBUtil.findOne(var0, var8, new Object[]{var9, var11});
                }
            } else {
                var2 = (Map)DynamicDBUtil.findOne(var0, var8, new Object[0]);
            }
        } else {
            List var6 = DynamicDBUtil.findList(var0, var1, new Object[0]);
            if (var6 != null && var6.size() > 0) {
                var2 = (Map)var6.get(0);
            }
        }

        return var2;
    }

    public static List<Map<String, Object>> a(String var0, String var1, String var2, int var3, int var4, Map<String, Object> var5) {
        DynamicDataSourceModel var6 = DataSourceCachePool.getCacheDynamicDataSourceModel(var1);
        String var7 = var2;
        DialectModel var8 = null;
        List var9 = null;
        if (!Boolean.valueOf(var0)) {
            DbType var10 = JdbcUtils.getDbType(var6.getDbUrl());
            IDialect var11 = DialectFactory.getDialect(var10);
            Page var12 = new Page((long)var3, (long)var4);
            var8 = var11.buildPaginationSql(var2, var12.offset(), var12.getSize());
            var7 = var8.getDialectSql();
        }

        if (var7.contains("?")) {
            long var15 = (Long)ReflectHelper.getFieldVal("firstParam", var8);
            long var16 = (Long)ReflectHelper.getFieldVal("secondParam", var8);
            int var14 = var7.length() - var7.replaceAll("\\?", "").length();
            if (var14 == 1) {
                var7 = org.jeecg.modules.online.a.a.a(var7, var15);
            } else {
                var7 = org.jeecg.modules.online.a.a.a(var7, var15, var16);
            }
        }
        log.info("RECORD_SQL====>"+var7);
        var9 = DynamicDBUtil.findListByNamedParam(var1, var7, var5);
        return var9;
    }
}
